//
//  DeviceOilLevelVC.m
//  QoilPublic
//
//  Created by SunChao on 2018/4/23.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "DeviceOilLevelVC.h"
#import "SingleOilLevelCell.h"
#import "SingleOilChartCell.h"
#import "FDAlertView.h"
#import "CustomDatePickerView.h"
@interface DeviceOilLevelVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong)UITableView *tbView;

@property (nonatomic,strong)NSMutableArray *dataSourceX;

@property (nonatomic,strong)NSMutableArray *dataSourceY;

@property (nonatomic,strong)NSString *checkDate;

@property (nonatomic,strong)UITextField *textField;
@end

@implementation DeviceOilLevelVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tbView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [self.view addSubview:self.tbView];
    
    [self.tbView registerNib:[UINib nibWithNibName:@"SingleOilLevelCell" bundle:nil] forCellReuseIdentifier:@"SingleOilLevelCell"];
    
     [self.tbView registerNib:[UINib nibWithNibName:@"SingleOilChartCell" bundle:nil] forCellReuseIdentifier:@"SingleOilChartCell"];
    
    [self.tbView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    
    self.tbView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectZero];
    
     NSString *currentTime = [self getLastDateTimes];
    
    self.checkDate = currentTime;
    
    [self requestALLDeviceDataWithDate:self.checkDate];
    
}

//获取当前的时间

-(NSString*)getLastDateTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
   
    NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:datenow];//前一天
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:lastDay];
    
    NSLog(@"currentTimeString =  %@",currentTimeString);
    
    return currentTimeString;
    
}
#pragma mark tableViewDelegate &&tableViewDataSource
- (void)requestALLDeviceDataWithDate:(NSString *)date{
    
    NSString *url = [QParamService urlWithIOTAction:@"report/historyOilLevel"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[QParamService getToken] forKey:@"token"];
    [param setObject:self.deviceID forKey:@"udId"];
   
    [param setObject:date forKey:@"datetime"];
    
    [[QHttpManager sharedManager] POST:url parameterMD5jm:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if (error) {
            
            NSLog(@"error:%@",basModel.errMsg);
            return ;
        }
        
        if([basModel.result isEqualToString:@"success"]){
            
            NSLog(@"dic:%@",basModel.items);
            self.dataSourceX = [basModel.items objectForKey:@"x"];
            self.dataSourceY = [basModel.items objectForKey:@"y"];
            
        }
        
        [self.tbView reloadData];
        
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        return 50;
    }
    if (indexPath.row == 1) {
        return 30;
    }
    return 300;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (indexPath.row == 0) {
        
         SingleOilLevelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SingleOilLevelCell"];
       
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
        cell.oilLevel.text = [NSString stringWithFormat:@"%@ %@",self.oilLevel,@"%"];
        
        return cell;
        
    }
   
    if (indexPath.row == 1) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        if (_textField == nil) {
              _textField = [[UITextField alloc]initWithFrame:CGRectMake((kScreenWidth-150)/2, 0, 150, 30)];
        }
        _textField.delegate = self;
        [cell addSubview:_textField];
        
        _textField.textAlignment = NSTextAlignmentCenter;
       _textField.text = [NSString stringWithFormat:@"%@",self.checkDate];
        [self setRightViewWithTextField:_textField imageName:@"日历"];
        return cell;
        
    }
    if (indexPath.row == 2) {
        
        SingleOilChartCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SingleOilChartCell"];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setCellWithDataX:self.dataSourceX dataY:self.dataSourceY];
        
        return cell;
        
    }
    return nil;
}

-(void)setRightViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    
    UIImageView *rightView = [[UIImageView alloc]init];
    rightView.image = [UIImage imageNamed:imageName];
    rightView.size = CGSizeMake(40, 40);
    rightView.contentMode = UIViewContentModeCenter;
    textField.rightView = rightView;
    textField.rightViewMode = UITextFieldViewModeAlways;
    
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    FDAlertView *alert = [[FDAlertView alloc] init];
    CustomDatePickerView * contentView=[[CustomDatePickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    contentView.delegate=self;
    alert.contentView = contentView;
    [alert show];
    return NO;

}

-(void)getTimeToValue:(NSString *)theTimeStr{
    
    _checkDate = theTimeStr;
    [self requestALLDeviceDataWithDate:_checkDate];
    
}
-(UITableView *)tbView{
    
    if (!_tbView) {
        
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tbView.delegate = self;
        _tbView.dataSource = self;
        _tbView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tbView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        
    }
    
    return _tbView;
    
}

ARRAY_INIT(dataSourceX);
ARRAY_INIT(dataSourceY);

@end
